<?php
/**
 * @description
 *  文章系统的管理入口
 *
 * filename : /admin/modules/content/content.php
 * date     : 2009-2-2 16:32
 * author   : hotgun
 * email    : hotgun@163.com
 * 
 * (c)copyright hotgun
 *
 * $Id: /admin/modules/content/content.php,v 1.0 2009-2-2 16:32 $
 */


$mod_url        = parse_mod_url();
$abs_modpath    = dirname(__FILE__);

$js_error       = 0;
$entrance       = load_module($mod, $action);

$tbl_main=$_TABLES['article'];

/**
 * +---------------------------------------------------------------------------+
 * 只有文章才可能操作专题，所以只在这里进行包含全局变量的工作
 *
 *
 */
// 包含专题缓存
$file_name  = $cfg_cache['cache_abspath'] . '/' . 
                $cfg_cache['content_special_cache_filename'];
$special_var= $cfg_cache['content_special_cache_var'];
if(file_exists($file_name)) {
    require($file_name);
} else {
    $$special_var = array();
}
// 包含专题分类缓存
$file_name  = $cfg_cache['cache_abspath'] . '/' . 
                $cfg_cache['content_specialcata_cache_filename'];
$specialcata_var= $cfg_cache['content_specialcata_cache_var'];
if(file_exists($file_name)) {
    require($file_name);
} else {
    $$specialcata_var = array();
}
/**
 * +---------------------------------------------------------------------------+
 */







// 默认提示设置，表示成功后默认显示添加和列表模板两种操作
// 默认跳转提示设置，表示成功后默认跳转到列表模板
$G_msg  = array(
    'jobs'  => array('add' => null, 'li' => null ),
    'redirect' => 'li'
    );
require($abs_modpath . '/pro.libs.php');
if($entrance != __FILE__) {
    // 子模块入口
    require($entrance);
} else {
    // 除了各子模块还有可能的action
    if(in_array($action, array('', 'index', 'li'))) {
        // 内容列表
        $viewvar    = & li();
        $cur_view   = 'views/pro_li_view.php';
    } elseif(in_array($action, array('add', 'modify', 'check'))) {
        if($save == $save_flag) {
            if(($action == 'add' && $rs = add()) && $rs['aid'] > 0) {
                $G_msg['msg'] = '新增成功！';
            } elseif(($action == 'modify' && $rs = modify()) && $rs['aid'] > 0) {
                $G_msg['msg'] = '修改成功！';
            } elseif(($action == 'check' && $rs = check()) && $rs['aid'] > 0) {
                $G_msg['msg'] = '审核成功！';
            } else {
                add_system_error();
            }

            if($rs['aid'] > 0) {
                // 如果是单页文章，直接跳转
                if($cata_info['cata_type'] == 2) {
                    redirect($mod_url['modify'] . "&cata=$cata&aid=" . $rs['aid']);
                    exit();
                }
                if($rs['gsid'] > 0) {
                    $G_msg['redirect'] = 's.ali';
                    $G_msg['redirect_param'] = '&sid=' . $rs['gsid'];
                    $G_msg['jobs']['modify']   = array(
                        'param' => 'aid=' . $rs['aid'] . '&sid=' . $rs['gsid'] ,
                        'name'  => '修改内容'
                        );
                } else {
                    $G_msg['jobs']['modify']   = array(
                        'param' => 'aid=' . $rs['aid'],
                        'name'  => '修改内容'
                        );
                }
                $G_msg['redirect_param'] = '&cid=' . $rs['cid'];
                message();
            }
        } else {
            InitGP(array('aid'), 'G');
            $viewvar    = & read($aid);
            $cur_view   = $action == 'check' ? 'views/content_check_view.php' : 'views/pro_data_view.php';
        }
    } elseif($action == 'checkbatch') {
        check_batch(1);
    } elseif($action == 'uncheckbatch') {
        check_batch(0);
    } elseif($action == 'move') {
        move_batch();
    } elseif($action == 'del') {
        del();
    } elseif($action == 'one') {
        // 新增/修改单页文章
        one($cata);
    } elseif($action == 'reorder') {
        reorder();
    } elseif('editf' == $action) {
		$param = array('id', 'act', 'val','isclass');
		InitGP($param,'G');
		$id = intval($id);
		$val = trim($val);
        $field  = '';
		if('order_no'==$field) {
			$val = bigint($val, 1);
		}

require($abs_modpath . '/pro_class.libs.php');

		if($id > 0 && $val!='') {
			$field = str_replace('edit_', '', $act);
			$conn   = & get_db_conn();

			if($isclass) {
				$tbl = $_TABLES['article_class'];
				$conn->Execute("update $tbl SET $field = '$val' WHERE cid = '$id' ");
				class_cache();
			} else {
				$tbl = $tbl_main;
				$conn->Execute("update $tbl SET $field = '$val' WHERE aid = '$id' ");
			}
			$result['content'] = $val;
		}
		if($ajax) {
			exit($json->encode($result));
		}
	} elseif('toggle' == $action) {
		// 如果是切换状态
		$fields = array('display', 'flag', 'flag1','guapai');	/* 允许更改状态的字段 */
		$param = array('id', 'val', 'f', 'cval');
		InitGP($param,'G');

		// 如果 $cval == 1 设置为 0
		// 如果 $cval == 0 设置为 1
		$id = intval($id);
		if(in_array($f,$fields)) {
			$val	= intval($val);
			$cval	= intval($cval);
			$newval	= 0;



			$conn   = & get_db_conn();
			// 得到老值
			$artflag = $conn->GetOne("SELECT $f FROM ".$tbl_main." WHERE aid='$id' ");
			$artflag = intval($artflag);


			if($cval==1) {
				// 去掉该标志
				$artflag = $artflag&(~$val);
			} else {
				// 增加该标志
				$artflag = $artflag|$val;
			}

			if($conn->Execute("UPDATE ".$tbl_main." SET $f='$artflag' WHERE aid='$id' ")) {
				$result['content'] = $cval==0 ? 1 : 0;
			}

		}
		if($ajax) {
			exit($json->encode($result));
		}
	} elseif('toggle1' == $action) {
		// 如果是切换状态
		$fields = array('display1');	/* 允许更改状态的字段 */
		$param = array('id', 'val', 'f', 'cval');
		InitGP($param,'G');

		// 如果 $cval == 1 设置为 0
		// 如果 $cval == 0 设置为 1
		$id = intval($id);
		if(in_array($f,$fields)) {
			$val	= intval($val);
			$cval	= intval($cval);
			$newval	= 0;



			$conn   = & get_db_conn();
			// 得到老值
			$artflag = $conn->GetOne("SELECT $f FROM ".$tbl_main." WHERE aid='$id' ");
			$artflag = intval($artflag);


			if($cval==1) {
				// 去掉该标志
				$artflag = $artflag&(~$val);
			} else {
				// 增加该标志
				$artflag = $artflag|$val;
			}

			if($conn->Execute("UPDATE ".$tbl_main." SET $f='$artflag' WHERE aid='$id' ")) {
				$result['content'] = $cval==0 ? 1 : 0;
			}

		}
		if($ajax) {
			exit($json->encode($result));
		}
	} elseif($action == 'pickup') {
        if($save == $save_flag) {
        } else {
            extract(pickup_read(@(int)$_GET['aid']), EXTR_OVERWRITE);
           // $cur_view   = 'views/content_pickup_data_view.php';
        }
    }
}
// 在载入视图之前，先查看错误集中是否有错误
check_error($js_error);
// 载入视图
require($cur_view);
?>